package com.pzl.mapper.impl;

import com.pzl.mapper.UserDao;
import com.pzl.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * UserDaoImpl class
 *
 * @Author panzhenglei
 * @Date 2019/6/8 9:37 AM
 */

@Repository
public class UserDaoImpl implements UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public List<User> findAll() throws Exception{

        String sql = "select * from sys_user";

        List<User> users = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));

       /* String sql2 = "SELECT ROLENAME FROM SYS_ROLE WHERE ID IN (SELECT ROLEID FROM SYS_USER_ROLE WHERE USERID = ?)";
        for (User user : users) {
           // List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql2, user.getId(), String.class);
           // List<String> query = jdbcTemplate.query(sql2, new BeanPropertyRowMapper<String>(String.class), user.getId());
            List<Map<String,Object>> query = jdbcTemplate.queryForList(sql2, user.getId(), String.class);
            String user_r ="";
            if (maps.size()>0){
                for (Map<String, Object> map : maps) {
                    Set<Map.Entry<String, Object>> entries = map.entrySet();
                    for (Map.Entry<String, Object> entry : entries) {
                        user_r+=entry.getValue();
                    }
                }
                user.setRoles(user_r);
            }
            System.out.println(query);
            String user_r ="";
            if (query.size()>0){
                for (String s : query) {
                    System.out.println(s);
                    user_r+=s;
                }
                user.setRoles(user_r);
                System.out.println(user_r);
            }

        }
*/
        return users;
    }

    @Override
    public int save(User user) {

        String sql = "INSERT INTO SYS_USER VALUES (?,?,?,?,?)";

        int i = jdbcTemplate.update(sql, user.getId(), user.getUsername(), user.getEmail(), user.getPassword(), user.getPhoneNum());

        return i;
    }

    @Override
    public int saveUserRole(User user,Long[] roleIds) {

        String sql = "INSERT INTO SYS_USER_ROLE VALUES (?,?)";
        for (Long roleId : roleIds) {
            Long userId = findUserId();
            jdbcTemplate.update(sql,userId,roleId);
        }


        return 0;
    }

    @Override
    public Long findUserId() {
       /* String sql = "SELECT ID FROM SYS_USER WHERE USERNAME = ? AND PASSWORD = ?";
        Long aLong = jdbcTemplate.queryForObject(sql, Long.class, user.getUsername(), user.getPassword());
*/
        String sql = "SELECT count(*) FROM SYS_USER";
        Long aLong = jdbcTemplate.queryForObject(sql, Long.class);
        return aLong;
    }

    @Override
    public User findOne(Long id) {
        String sql = "SELECT * FROM SYS_USER WHERE ID = ?";


        User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), id);

        return user;
    }

    @Override
    public int update(User user) {
        String sql = "UPDATE SYS_USER SET USERNAME = ? ,EMAIL = ?,PASSWORD = ? ,PHONENUM = ? WHERE ID = ?";

        int i = jdbcTemplate.update(sql,  user.getUsername(), user.getEmail(), user.getPassword(), user.getPhoneNum(),user.getId());

        return i;

    }
}
